package com.example.readbook.Adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bifan.txtreaderlib.main.TxtConfig
import com.bifan.txtreaderlib.ui.PlayActivity
import com.example.readbook.Bean.BookBean
import com.example.readbook.R
import com.example.readbook.Sql.SqliteUtils
import com.example.readbook.Utils.Utils
import kotlinx.android.synthetic.main.item_main_book.view.*
import java.io.File

/**
 * Package    : com.example.readbook.Adapter
 * ClassName  : BookAdapter
 * Description: ${TODO}
 * Date       : 2019/3/22 11:55
 */
class BookAdapter(val context: Context, var mList: MutableList<BookBean>) : BaseAdapter<BookBean>(mList) {
    var isShowAll = false
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyHolder {
        return RecyHolder(LayoutInflater.from(context).inflate(R.layout.item_main_book, p0, false))
    }

    override fun getItemCount(): Int {
        return if (mList.size % 3 == 0) mList.size / 3 else mList.size / 3 + 1
    }

    override fun onBindViewHolder(holder: RecyHolder, p1: Int) {

        val bean1 = mList[p1 * 3]
        if (p1 * 3 > mList.size - 1) {
            holder.itemView.Item_Main_Del1.visibility = View.INVISIBLE
            holder.itemView.Item_Main_txt1.visibility = View.INVISIBLE
            holder.itemView.Item_Main_BookBack1.visibility = View.INVISIBLE
            holder.itemView.Item_Main_BookName1.visibility = View.INVISIBLE
            return
        } else {
            holder.itemView.Item_Main_BookBack1.visibility = View.VISIBLE
            holder.itemView.Item_Main_BookName1.visibility = View.VISIBLE
        }
        if (bean1.isChoose)
            holder.itemView.Item_Main_Del1.visibility = View.VISIBLE
        else holder.itemView.Item_Main_Del1.visibility = View.GONE
        holder.itemView.Item_Main_txt1.visibility = View.VISIBLE
        setImage(holder.itemView.Item_Main_BookBack1, bean1.color)
        holder.itemView.Item_Main_BookName1.text = bean1.bookName
        holder.itemView.Item_Main_Del1.setOnClickListener {
            delete(bean1)
        }
        holder.itemView.Item_Main_Gen1.setOnClickListener {
            TxtConfig.saveIsOnVerticalPageMode(context, false)
            if (TextUtils.isEmpty(bean1.bookAddress) || !File(bean1.bookAddress).exists()) {
                Utils.showToast("文件不存在")
                delete(bean1)
            } else {
                PlayActivity.loadTxtFile(context, bean1.bookAddress)
            }
        }
        holder.itemView.Item_Main_Gen1.setOnLongClickListener {
            showAllDelete(!isShowAll)
            true
        }


        if (p1 * 3 + 1 > mList.size - 1) {
            holder.itemView.Item_Main_Del2.visibility = View.INVISIBLE
            holder.itemView.Item_Main_txt2.visibility = View.INVISIBLE
            holder.itemView.Item_Main_BookBack2.visibility = View.INVISIBLE
            holder.itemView.Item_Main_BookName2.visibility = View.INVISIBLE
            return
        } else {
            holder.itemView.Item_Main_BookBack2.visibility = View.VISIBLE
            holder.itemView.Item_Main_BookName2.visibility = View.VISIBLE
        }
        val bean2 = mList[p1 * 3 + 1]
        if (bean2.isChoose)
            holder.itemView.Item_Main_Del2.visibility = View.VISIBLE
        else holder.itemView.Item_Main_Del2.visibility = View.GONE
        holder.itemView.Item_Main_txt2.visibility = View.VISIBLE
        setImage(holder.itemView.Item_Main_BookBack2, bean2.color)
        holder.itemView.Item_Main_BookName2.text = bean2.bookName
        holder.itemView.Item_Main_Del2.setOnClickListener {
            delete(bean2)
        }
        holder.itemView.Item_Main_Gen2.setOnClickListener {
            TxtConfig.saveIsOnVerticalPageMode(context, false)
            if (TextUtils.isEmpty(bean2.bookAddress) || !File(bean2.bookAddress).exists()) {
                Utils.showToast("文件不存在")
                delete(bean2)
            } else {
                PlayActivity.loadTxtFile(context, bean2.bookAddress)
            }
        }
        holder.itemView.Item_Main_Gen2.setOnLongClickListener {
            showAllDelete(!isShowAll)
            true
        }



        if (p1 * 3 + 2 > mList.size - 1) {
            holder.itemView.Item_Main_Del3.visibility = View.INVISIBLE
            holder.itemView.Item_Main_txt3.visibility = View.INVISIBLE
            holder.itemView.Item_Main_BookBack3.visibility = View.INVISIBLE
            holder.itemView.Item_Main_BookName3.visibility = View.INVISIBLE
            return
        } else {
            holder.itemView.Item_Main_BookBack3.visibility = View.VISIBLE
            holder.itemView.Item_Main_BookName3.visibility = View.VISIBLE
        }

        val bean3 = mList[p1 * 3 + 2]
        if (bean3.isChoose)
            holder.itemView.Item_Main_Del3.visibility = View.VISIBLE
        else holder.itemView.Item_Main_Del3.visibility = View.GONE
        holder.itemView.Item_Main_txt3.visibility = View.VISIBLE
        setImage(holder.itemView.Item_Main_BookBack3, bean3.color)
        holder.itemView.Item_Main_BookName3.text = bean3.bookName
        holder.itemView.Item_Main_Del3.setOnClickListener {
            delete(bean3)
        }
        holder.itemView.Item_Main_Gen3.setOnClickListener {
            TxtConfig.saveIsOnVerticalPageMode(context, false)
            if (TextUtils.isEmpty(bean3.bookAddress) || !File(bean3.bookAddress).exists()) {
                Utils.showToast("文件不存在")
                delete(bean3)
            } else {
                PlayActivity.loadTxtFile(context, bean3.bookAddress)
            }
        }
        holder.itemView.Item_Main_Gen3.setOnLongClickListener {
            showAllDelete(!isShowAll)
            true
        }
    }

    private fun delete(id: BookBean) {
        SqliteUtils.getInstence(context).delete(id.id.toString())
        updateList(SqliteUtils.getInstence(context).readAllBook())
        showAllDelete(isShowAll)
    }

    private fun setImage(img: ImageView, src: String) {
        when (src) {
            "0" -> {
                img.setBackgroundResource(R.mipmap.a1)
            }
            "1" -> {
                img.setBackgroundResource(R.mipmap.a2)
            }
            else -> {
                img.setBackgroundResource(R.mipmap.a3)
            }
        }
    }

    fun showAllDelete(boolean: Boolean) {
        mList.forEach {
            it.isChoose = boolean
        }
        isShowAll = boolean
        notifyDataSetChanged()
    }
}
